package main

import (
	"fmt"
	"log"
	"net"
	"os"
	"port-scanner/task"
	"port-scanner/util"
	"port-scanner/vars"
)

func main() {
	ipList, ports := parseArgs()

	task.Run(ipList, ports)

	fmt.Println("\nScan results")
	vars.Result.Range(func(k, v any) bool {
		fmt.Printf("%s:\t%v\n", k, v)
		return true
	})
}

func parseArgs() ([]net.IP, []int) {
	if len(os.Args) < 3 {
		fmt.Println("Usage")
		fmt.Println("Single scan:\n\tport-scanner 0.0.0.0 22")
		fmt.Println("Range scan:\n\tport-scanner 0.0.0.0/24 22-80")
		fmt.Println("\tport-scanner 0.0.0.0-255 22-80,9000-9001")
		fmt.Println("\tport-scanner 0.0.0.* 22-80,8080,9000-9001")
		log.Fatalln("Args error")
	}

	ipList, err := util.ParseIpList(os.Args[1])
	if err != nil {
		log.Fatalln("Parse ip error:", err)
	}

	ports, err := util.ParsePorts(os.Args[2])
	if err != nil {
		log.Fatalln("Parse port error:", err)
	}
	return ipList, ports
}
